﻿using AdverseEventReportSystem.BLL;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Model;
using AdverseEventReportSystem.Service;
using AdverseEventReportSystem.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AdverseEventReportSystem.Web.Areas.AdverseEvent
{
    public class AdverseEventTypeController : BaseController
    {
        // GET: AdverseEvent/AdverseEventType
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 事件类型列表
        /// </summary>
        /// <returns></returns>
        public ActionResult Search()
        {
            int pageIndex = DataConvert.StrToInt(Request["page"], 1);
            int pageSize = DataConvert.StrToInt(Request["pageSize"], ConfigHelper.GetConfigInt("pagesize"));
            string whereSql = GetWhereSql();
            SelectBuilder data = new SelectBuilder();
            data.Having = "";
            data.GroupBy = "";
            data.OrderBy = "";
            data.From = "AdverseEventType";
            data.Select = "ID,Name,Remark";
            data.WhereSql = whereSql;
            data.PagingCurrentPage = pageIndex;
            data.PagingItemsPerPage = pageSize;
            int total = 0;
            DataTable dt = BaseService<AdverseEventType>.GetQueryManyForPage_Proc(data, out total);
            var result = new { Total = total, Rows = dt };
            return JsonDate(result);
        }

        /// <summary>
        /// 获取查询条件
        /// </summary>
        /// <returns></returns>
        private string GetWhereSql()
        {
            string name = DataConvert.ToString(Request["name"]);
            string whereSql = "1 = 1";
            if (name != "")
            {
                whereSql += string.Format(" and Name Like '%{0}%'", name);
            }
            return whereSql;
        }

        /// <summary>
        /// 添加/编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(int id = 0)
        {
            AdverseEventType adverseEventType = new AdverseEventType();
            if (id > 0)
            {
                adverseEventType = AdverseEventTypeService.GetQuerySingleByID(id);
            }
            ViewBag.AdverseEventType = adverseEventType;
            return View();
        }

        /// <summary>
        /// 添加/编辑事件类型
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Save(AdverseEventType adverseEventType)
        {
            OperateResult resultInfo = AdverseEventTypeManager.Save(adverseEventType);
            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 删除事件类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(int id)
        {
            OperateResult resultInfo = AdverseEventTypeManager.Delte(id);
            return JsonDate(resultInfo);
        }
    }
}